package _6_socket.tcp;

import java.io.*;
import java.net.Socket;

/**
 * Created by bpzj on 2019-01-18.
 */
public class TcpClientBySocket {

    //客户端
    public static void main(String[] args) throws IOException {

        //1、创建客户端Socket，指定服务器地址和端口
        Socket socket =new Socket("localhost",10086);
        //2、socket输出流，_6_socket client 向 server 输出的 流
        OutputStream socketOutput = socket.getOutputStream();
        PrintWriter pw =new PrintWriter(socketOutput); //将输出流包装成打印流
        pw.write("用户名：admin；密码：123");
        pw.flush();
        socket.shutdownOutput();
        //3、获取输入流，并读取服务器端的响应信息
        InputStream is = socket.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String info = null;

        while((info=br.readLine()) != null){
            System.out.println("我是客户端，服务器说："+info);
        }

        //4、关闭资源
        br.close();
        is.close();
        pw.close();
        socketOutput.close();
        socket.close();
    }

}
